package com.mall.product.controller;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.repository.query.Param;

import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.CategoryService;
import com.mall.common.utils.Result;


@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @GetMapping("/list/tree")
    public Result treeList() {
        List<CategoryEntity> categories = categoryService.listWithTree();
        return Result.ok().put("data", categories);
    }

//    /**
//     * 列表
//     */
//    @RequestMapping("/list")
//    public Result list(){
//        return Result.ok().put("page", categoryService.list());
//    }


//    /**
//     * 信息
//     */
//    @RequestMapping("/info/{catId}")
//    public Result info(@PathVariable("catId") Long catId) {
//        CategoryEntity category = categoryService.getById(catId);
//
//        return Result.ok().put("category", category);
//    }

    /**
     * 根据id查询
     */
    @GetMapping("/getCategory")
    public Result getCategoryID(@Param("catId") Long catId){
        CategoryEntity category = categoryService.getById(catId);

        return Result.ok().put("data", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update/sort")
    public Result updateSort(@RequestBody List<CategoryEntity> categoryEntityList) {
        categoryService.updateBatchById(categoryEntityList);

        return Result.ok();
    }

//    /**
//     * 删除
//     * @RequestBody:获取请求体，必须发送POST请求
//     * SpringMVC自动将请求体的数据（json），转为对应的对象
//     */
//    @RequestMapping("/delete")
//    public Result delete(@RequestBody Long[] catIds){
//		categoryService.removeByIds(Arrays.asList(catIds));
//
//        return Result.ok();
//    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Long> ids) {
        categoryService.removeMenuByIds(ids);
        return Result.ok();
    }

}
